﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

namespace ChinessChess
{
   public class CGame
    {
       
        private ChessColor m_Color;
        private ChessType m_Type;
        
        public ChessColor Color{
            get { return m_Color; }
            set { m_Color = value; }
        }
        
        public ChessType Type{
            get { return m_Type; }
            set { m_Type = value; }
        }

        public static Boolean CheckQTuong(int wIndex, int bIndex) {
            int i = wIndex;
            Boolean isOptions = false;
            while (i < bIndex)
            {
                i += 9;
                if (i == bIndex)
                {
                    isOptions = true;
                }
            }
            return isOptions;
        }
       //Ham kiem tra 2 tuong doi mat nhau
        public static Boolean CheckQTuong(COCo []flag,int wIndex,int bIndex) {
           
            Boolean isOptions = CheckQTuong(wIndex,bIndex);
            //hai tuong doi mat nhau kiem tra xem co quan o giua hay khong
            if (isOptions) {
                for (int index = wIndex + 9; index <= bIndex; index += 9) {
                    if (flag[index].State == true) {
                        return false;
                    }
                }
            }
            return isOptions;
        }

        public static Boolean CheckNuocDi(COCo[] flag, int newIndex, int bIndex) {
            Boolean isCheck = false;
            //
            if (flag[newIndex].Cot == flag[bIndex].Cot || flag[newIndex].Dong == flag[bIndex].Dong)
            {
                isCheck = true;
            }
            
            return isCheck;
        }

        public static Boolean CheckQTuong(COCo[] flag, int wIndex, int bIndex, int indexOld)
        {

            Boolean isOptions = CheckQTuong(wIndex, bIndex);
            
            
            //hai tuong doi mat nhau kiem tra xem co quan o giua hay khong
            if (isOptions)
            {
                for (int index = wIndex + 9; index < bIndex; index += 9)
                {
                    if (flag[index].State == true && index != indexOld)
                    {
                        return false;
                    }
                }
            }
            return isOptions;

        }
        public static Boolean CheckQTuong(COCo[] flag, int wIndex, int bIndex, int indexOld,int newIndex)
        {

            Boolean isOptions = CheckQTuong(wIndex, bIndex);
            Boolean temp = flag[newIndex].State;
            temp = !temp;
            //hai tuong doi mat nhau kiem tra xem co quan o giua hay khong
            if (isOptions)
            {
                for (int index = wIndex + 9; index < bIndex; index += 9)
                {
                    if (flag[index].State == true && index != indexOld )
                    {
                        return false;
                    }
                }
            }
            return isOptions;
        }

        public static Boolean KiemTraChieuTuong(ChessType type, COCo[] flag, int gIndex, int qIndex) {
            Boolean isCheck = false;
            switch (type) {
                case ChessType.CMa: {
                    isCheck = CheckMa(flag, gIndex, qIndex);
                    break;
                }
                case ChessType.CPhao: {
                    isCheck = CheckPhao(flag, gIndex, qIndex);
                    break;
                }
                case ChessType.CTot: {
                    isCheck = CheckTot(flag, gIndex, qIndex);
                    break;
                }
                case ChessType.CXe: {
                    isCheck = CheckXe(flag, gIndex, qIndex);
                    break;
                }
            }
            return isCheck;
        }

        private static Boolean CheckTot(COCo[] flag, int gIndex, int qIndex) {
            Boolean isCheck = false;
            if (Math.Abs(gIndex - qIndex) == 9 || Math.Abs(gIndex - qIndex) == 1){
                isCheck = true;
            }
            return isCheck;
        }


       
       /// <summary>
       /// Khi di chuyen con ma kiem tra xem co chieu tuong ko?
       /// </summary>
       /// <param name="flag"></param>
       /// <param name="gIndex"></param>
       /// <param name="qIndex"></param>
       /// <returns></returns>
        private static Boolean CheckMa(COCo[] flag, int gIndex, int qIndex){
            Boolean isCheck =false;
            int dong = flag[gIndex].Dong - flag[qIndex].Dong;
            int cot = flag[gIndex].Cot - flag[qIndex].Cot;
            if ((Math.Abs(dong) == 1 && Math.Abs(cot) == 2) || (Math.Abs(dong) == 2 && Math.Abs(cot) == 1))
            {
                isCheck = true;
            }
            if (isCheck)
            {
                isCheck = false;
                int vt = flag[gIndex].Dong - flag[qIndex].Dong;
                vt = Math.Abs(vt);
                if (vt == 1)
                {

                    if (cot > 0)
                    {
                        if (flag[qIndex + 1].State == false)
                        {
                            isCheck = true;
                        }
                    }
                    else
                    {
                        if (flag[qIndex - 1].State == false)
                        {
                            isCheck =true;
                        }
                    }
                }
                else
                {

                    if (dong > 0)
                    {
                        if (flag[qIndex + 9].State == false)
                        {
                            isCheck = true;
                        }
                    }
                    else
                    {
                        if (flag[qIndex - 9].State == false)
                        {
                            isCheck = true;
                        }
                    }
                }
            }
            return isCheck;
        }
       /// <summary>
       /// Khi di chuyen con phao kiem tra co chieu tuong ko
       /// </summary>
       /// <param name="flag"></param>
       /// <param name="gIndex"></param>
       /// <param name="qIndex"></param>
       /// <returns></returns>
        private static Boolean CheckPhao(COCo[] flag, int gIndex, int qIndex) {
            Boolean isCheck =false;
            if (flag[gIndex].Dong == flag[qIndex].Dong || flag[gIndex].Cot == flag[qIndex].Cot)
            {
                isCheck = true;
            }
            if (isCheck)
            {
                if (flag[gIndex].Dong == flag[qIndex].Dong)
                {
                    isCheck = false;
                    int index = qIndex;
                    int cout = 0;
                    if (gIndex < qIndex)
                    {
                        while (index > gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index -= 1;
                        }
                    }
                    else
                    {
                        while (index < gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index += 1;
                        }
                    }
                    if (cout == 2)
                    {
                        isCheck = true;
                    }
                }
                else
                { //Phao di ngang thi cung nam tren 1 cot
                    isCheck = false;
                    int index = qIndex;
                    int cout = 0;
                    if (gIndex < qIndex)
                    {
                        while (index > gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index -= 9;
                        }
                    }
                    else
                    {
                        while (index < gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index += 9;
                        }
                    }
                    if (cout == 2)
                    {
                        isCheck = true;
                    }
                }
            }
            return isCheck;
        }

       /// <summary>
       /// Khi di chuyen con xe kiem tra co chieu tuong ko
       /// </summary>
       /// <param name="flag"></param>
       /// <param name="gIndex"></param>
       /// <param name="qIndex"></param>
       /// <returns></returns>
        private static Boolean CheckXe(COCo[] flag, int gIndex, int qIndex)
        {
            Boolean isCheck = false;
            if (flag[gIndex].Dong == flag[qIndex].Dong || flag[gIndex].Cot == flag[qIndex].Cot)
            {
                isCheck = true;
            }
            if (isCheck)
            {
                if (flag[gIndex].Dong == flag[qIndex].Dong)
                {
                    int index = qIndex;//vi tri cua quan tuong
                    int cout = 0;
                    if (gIndex > qIndex)
                    {
                        while (index > gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index++;
                        }
                    }
                    else
                    {
                        while (index < gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index--;
                        }
                    }
                    if (cout == 1)
                    {
                        return true;
                    }
                }
                else
                {
                    int index = qIndex;//vi tri cua quan tuong
                    int cout = 0;
                    if (gIndex > qIndex)
                    {
                        while (index > gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index += 9;
                        }
                    }
                    else
                    {
                        while (index < gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index -= 9;
                        }
                    }
                    if (cout == 1)
                    {
                        return true;
                    }
                }
            }
            return isCheck;
        }

    }
}
